package com.example.sy3;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "CircleAreaServlet", value = "/CircleAreaServlet")
public class CircleAreaServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("<html><body  bgcolor='pink'>");
        String strRadius = request.getParameter("strRadius"); // 获取半径
        double r = 0, area = 0, circumference = 0;
        try {
            r = Double.parseDouble(strRadius);
            area = 3.1415926*r*r                    ;// 计算面积
            circumference = 3.1415926*2*r                    ;// 计算周长
            out.print("<BR>半径:" + r );
            out.print("<BR>圆面积:" + area );
            out.print("<BR>圆周长:" + circumference );
            out.println("</body></html>");
        } catch (Exception e) {
            request.setAttribute(strRadius, strRadius + ":半径数据不合法");
            // 请求转发到另一个servlet进行查询响应
            RequestDispatcher rd = request.getRequestDispatcher("/ErrorServlet")                    ;
            rd.forward(request, response);
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}